package nc.gk.gkxm.controller.api;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import nc.gk.gkxm.model.dto.TopicDto;
import nc.gk.gkxm.model.vo.PersonlibVo;
import nc.gk.gkxm.model.vo.TopicVO;
import nc.gk.gkxm.service.TopicService;
import nc.gk.gkxm.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "题目类提供小程序接口")
@RestController
@RequestMapping("/gkxm/api/topic")
public class TopicController {
    @Autowired
    private TopicService topicService;
    @ApiOperation("查询接口")
    @PostMapping("query")
    public Result getTopic(@RequestBody TopicDto topicDto){
        try {
            List<TopicVO> vos = topicService.getAllTopic();
            return Result.ok(vos);
        } catch (Exception e) {
            return Result.fall(5001,e.getMessage());
        }

    }


    /**
     * 根据对应的分类来查找不通的题目
     * @param sortIds
     * @return
     */
    @ApiOperation("根据对应的分类来查找不通的题目")
    @GetMapping("search")
    public Result getTopics(Integer[] sortIds){
//       异常查询失败 未异常查询成功  如果是null or 空字符串则显示维护信息
      try{
          List<TopicVO> data=topicService.getAllBySortId(sortIds);
          if(data.size()==0){
              return Result.fall(200,"暂无数据");
          }
          return Result.ok(data);
      }catch (Exception e){
          return Result.fall(5001,e.getMessage());
      }


    }
    @ApiOperation("根据题目的内容进行查找")
    @GetMapping("search/{content}")
    public Result getTopics(@PathVariable("content")String content) {
        try {
            List<TopicVO> vos = topicService.getAllByContent(content);
            return Result.ok(vos);
        } catch (Exception e) {
            return Result.fall(5001, e.getMessage());
        }
    }

    @ApiOperation("获得答案和解析")
    @GetMapping("analyze/{topicId}")
    public Result getAnalyze(@PathVariable("topicId")Integer topicId){
        Map<String,String> data=topicService.getAnalyze(topicId);
        return Result.ok(data);
    }

    @ApiOperation("答案是否正确")
    @GetMapping("answer/{topicId}")
    public Result getAnswer( TopicDto topicDto){
//        1正确 2错误
        Integer result=topicService.getAnswer(topicDto);
        return Result.ok(result);
    }
//    前端做 前端保存题id和答案
//    后端返回错误的答案id和正确的答案和解析给前端
//    后端保存此次用户做的试题和他填写的答案
    @ApiOperation("零时保存记录接口")
    @PostMapping("save")
    public Result save(@RequestBody TopicDto topicDto){
        return Result.ok("新增成功");
    }

    @ApiOperation("新增接口")
    @PostMapping("insert")
    public Result insert(@RequestBody TopicDto topicDto){
        topicService.insertTopic(topicDto);
        return Result.ok("新增成功");
    }
    @ApiOperation("修改接口")
    @PostMapping("update")
    public Result update(@RequestBody TopicDto topicDto){
        topicService.updateTopic(topicDto);
        return Result.ok("修改成功");
    }

    @ApiOperation("删除接口")
    @DeleteMapping("delete/{topicId}")
    public Result delete(@PathVariable("topicId")Integer topicId){
        topicService.deleteByTopicId(topicId);
        return Result.ok("删除成功");
    }

    @ApiOperation("获取随机一个没做的题")
    @GetMapping
    public Result getOneRandomTopic(@RequestHeader("token")String token){
        PersonlibVo data=topicService.getOneRandomTopic(token);
        return Result.ok(data);
    }
}
